window.onload = function() {
};

function getXmlHttpRequest() {
	if (typeof XMLHttpRequest == "undefined")
		XMLHttpRequest = function() {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP.6.0");
			} catch (e) {
			}
			try {
				return new ActiveXObject("Msxml2.XMLHTTP.3.0");
			} catch (e) {
			}
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			}
			// Microsoft.XMLHTTP points to Msxml2.XMLHTTP.3.0 and is redundant
			throw new Error("This browser does not support XMLHttpRequest.");
		};
	return new XMLHttpRequest();
}

/**
 * Update a game, send ajax request, calculate who's turn will next, ensure end game is no other move to player/computer
 * @param who 
 * @param cell number of cell
 */
function updateGame(who, cell) {
	if (isTimerOn) {
			var xhr = getXmlHttpRequest();
			var valid = false;
			
			// set parameters
			var params = "parameter=update";
			if (who == "player" && document.getElementById("Player").getElementsByTagName("td")[cell].getElementsByTagName("input")[0].value > 0) {
				params = params + "&player=" + cell;
				valid = true;
			} else if (who == "computer" && document.getElementById("Computer").getElementsByTagName("td")[cell].getElementsByTagName("input")[0].value > 0) {
				params = params + "&computer=" + cell;
				valid = true;
			} else {
				valid = false;
			}
			
			if (valid) {
				xhr.open("post", "AjaxHandler", true);
				xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xhr.onreadystatechange = function() {
					if (xhr.readyState == 4 && xhr.status == 200) {
						var result = JSON.parse(xhr.responseText);
						
						var computer = document.getElementById("Computer");
						var player = document.getElementById("Player");
			
						// update table with response
						updateComputer(computer, result);
						updatePlayer(player, result);
						
						// check end of game
						if (who == "player") {
							canMove("player");
						} else {
							canMove("computer");
						}
						
						// set table for next turn, if computer turn, move with it
						var nextTurn = result.nextTurn; 
						if (nextTurn == "player") {
							disableComputerButtons();
							enablePlayerButtons();
						} else if (nextTurn == "computer"){
							disablePlayerButtons();
							enableComputerButtons();
							moveComputer();
						}
					}
				};
				xhr.send(params);
			}
	}
	
}

/**
 * Initialize a game on server side with AJAX request
 */
function newGame() {
	var xhr = getXmlHttpRequest();

	var params = "parameter=init";
	xhr.open("post", "AjaxHandler", true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			console.log("ajax response");
			var result = JSON.parse(xhr.responseText);
			
			var computer = document.getElementById("Computer");
			var player = document.getElementById("Player");

			updateComputer(computer, result);
			updatePlayer(player, result);

			disableComputerButtons();
			enablePlayerButtons();
		}
	};
	console.log("ajax request");
	xhr.send(params);
}

function computerTurn(cell) {
	updateGame("computer", cell);
	return false;
}

function playerTurn(cell) {
	updateGame("player", cell);
	return false;
}

